package com.dayu.sale.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.SecurityUtils;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.domain.constant.GoodsConstant;
import com.dayu.recovery.mapper.RecoveryGoodsMapper;
import com.dayu.sale.domain.DyBillInfo;
import com.dayu.sale.domain.DySaleGoods;
import com.dayu.sale.mapper.DyBillInfoMapper;
import com.dayu.sale.mapper.DySaleGoodsMapper;
import org.springframework.stereotype.Service;
import com.dayu.sale.mapper.DyRefundInfoMapper;
import com.dayu.sale.domain.DyRefundInfo;
import com.dayu.sale.service.IDyRefundInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 版本3 --- 退单信息Service业务层处理
 *
 * @author HK
 * @date 2024-01-10
 */
@Service
public class DyRefundInfoServiceImpl implements IDyRefundInfoService
{
    @Resource
    private DyRefundInfoMapper dyRefundInfoMapper;

    @Resource
    private DySaleGoodsMapper saleGoodsMapper;

    @Resource
    private RecoveryGoodsMapper recoveryGoodsMapper;

    @Resource
    private DyBillInfoMapper billInfoMapper;

    /**
     * 查询版本3 --- 退单信息
     *
     * @param id 版本3 --- 退单信息ID
     * @return 版本3 --- 退单信息
     */
    @Override
    public DyRefundInfo selectDyRefundInfoById(Long id)
    {
        return dyRefundInfoMapper.selectDyRefundInfoById(id);
    }

    /**
     * 查询版本3 --- 退单信息列表
     *
     * @param dyRefundInfo 版本3 --- 退单信息
     * @return 版本3 --- 退单信息
     */
    @Override
    public List<DyRefundInfo> selectDyRefundInfoList(DyRefundInfo dyRefundInfo)
    {
        List<DyRefundInfo> list = dyRefundInfoMapper.selectDyRefundInfoList(dyRefundInfo);
        list.forEach(it -> {
            if(!ObjectUtils.isEmpty(it.getBillId())) {
                DyBillInfo bill = billInfoMapper.selectDyBillInfoById(it.getBillId());
                if(!ObjectUtils.isEmpty(bill))
                    it.setSaleName(bill.getSaleUserName());
            }
        });
        return list;
    }

    /**
     * 新增版本3 --- 退单信息
     *
     * @param bo 版本3 --- 退单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDyRefundInfo(DyRefundInfo bo)
    {
        DySaleGoods goods = saleGoodsMapper.selectDySaleGoodsById(bo.getSaleGoodsId());
        if(ObjectUtils.isEmpty(goods))
            throw new RuntimeException("未获取到商品信息！");
        if(goods.getHasRefund() == 1)
            throw new RuntimeException("商品已退货，不可操作");
        bo.setBillId(goods.getBillId());
        bo.setGoodsId(goods.getGoodsId());
        bo.setGoodsName(goods.getGoodsName());
        bo.setSalePrice(goods.getSalePrice());
        bo.setStatus(1);
        bo.setProfitPrice(bo.getSalePrice().subtract(bo.getRefundPrice()));
        bo.setCreateTime(DateUtils.getNowDate());
        bo.setCreateBy(SecurityUtils.getUsername());

        goods.setHasRefund(1);
        saleGoodsMapper.updateDySaleGoods(goods);
        return dyRefundInfoMapper.insertDyRefundInfo(bo);
    }

    @Override
    @Transactional
    public int refundPass(Long id, String remark) {
        DyRefundInfo info = dyRefundInfoMapper.selectDyRefundInfoById(id);
        if (ObjectUtils.isEmpty(info))
            throw new RuntimeException("没有获取到退单信息");
        info.setStatus(2);
        info.setUpdateBy(SecurityUtils.getUsername());
        info.setUpdateTime(new Date());
        info.setCheckTime(info.getUpdateTime());
        info.setCheckUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        info.setCheckUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        info.setCheckRemark(remark);
        List<String> goodsIds = new ArrayList<>();
        goodsIds.add(info.getGoodsId());
        recoveryGoodsMapper.editGoodsStatusByList(goodsIds, GoodsConstant.GOODS_STATUS_OK); //设置为正常状态
        return dyRefundInfoMapper.updateDyRefundInfo(info);
    }

    @Override
    @Transactional
    public int refundNoPass(Long id, String remark) {
        DyRefundInfo info = dyRefundInfoMapper.selectDyRefundInfoById(id);
        if (ObjectUtils.isEmpty(info))
            throw new RuntimeException("没有获取到退单信息");
        info.setStatus(3);
        info.setUpdateBy(SecurityUtils.getUsername());
        info.setUpdateTime(new Date());
        info.setCheckTime(info.getUpdateTime());
        info.setCheckUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        info.setCheckUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        info.setCheckRemark(remark);
        DySaleGoods goods = saleGoodsMapper.selectDySaleGoodsById(info.getSaleGoodsId());
        goods.setHasRefund(0);
        saleGoodsMapper.updateDySaleGoods(goods);
        return dyRefundInfoMapper.updateDyRefundInfo(info);
    }
}
